import {
  ds
} from "~/tools/data"
import {
  ui
} from "~/tools/ui"
import {
  nav2
} from "~/tools/nav2"
import {
  pages
} from "~/tools/pages"
import {
  users
} from "~/tools/users"

Page({
  data: {
    list: [],
    locText: "",
    loc: [],
    errMsg: "",
    stepFee: 0,
    selectedShop: 0,
    user: {}
  },
  async init() {
    const that = this,
      _ = ds.cmd
    ds.coll('help')
      .where({
        _id: _.exists(true)
      })
      .orderBy('date', 'asc')
      .get({
        success: function (res) {
          that.setData({
            list: res.data
          })
        }
      });
    this.loadUser()
  },
  loadUser() {
    users.getUserInfo().then(user => {
      this.setData({
        user
      })
    })
  },
  editMod(e) {
    const mod = ui.eventData(e, 'mod');
    const {
      user
    } = this.data
    nav2.navigate(`/pages/usercenter/help/detail/index?data=${JSON.stringify(mod)}${user.role=='admin'?'&action=edit':''}`);
  },
  backTap() {
    pages.back()
  },
  addModule(e) {
    nav2.navigate(`/pages/usercenter/help/detail/index?action=edit&data=${JSON.stringify({})}`);
  },
  changeVisible(e) {
    const mod = ui.eventData(e, "mod")
    ui.loading()
    const visible = (typeof mod.visible == 'undefined') ? false : !mod.visible
    ds.cloudUpdate('module', mod._id, {
      visible
    }, res => {
      ui.loading(false);
      const success = res.result.errMsg.endsWith(":ok")
      ui.toast(success)
      if (success) this.init()
    })
  },
  onShow() {},
  onLoad() {
    this.init(true);
  },
  onDelete(e) {
    const mod = ui.eventData(e, 'mod')
    ui.confirmx(`是否删除？`).then(res => {
      if (!res) return;
      ui.loading()
      ds.cloudRemove('help', {
        _id: mod.id || mod._id
      }, res => {
        const succ = res.result.errMsg.endsWith(":ok")
        ui.loading(false)
        ui.toast(succ)
        succ && this.init()
      })
    })
  }
});